package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.format.DateTimeFormatter;
import java.util.List;

@Controller
public class AlternativeUserManagementController {

    @Autowired
    private UserService userService;

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/alternative-user-management")
    @ResponseBody
    public String showUserManagementPage(Authentication authentication) {
        // 获取当前登录用户
        String username = authentication.getName();
        User currentUser = userService.findByUsername(username);
        
        // 获取所有用户列表
        List<User> users = userService.findAll();
        
        // 创建HTML响应 - 使用英文内容避免中文编码问题
        StringBuilder html = new StringBuilder();
        html.append("<!DOCTYPE html>");
        html.append("<html>");
        html.append("<head>");
        html.append("    <meta charset='UTF-8'>");
        html.append("    <title>User Management</title>");
        html.append("</head>");
        html.append("<body>");
        html.append("    <div>");
        html.append("        <h2>User Management</h2>");
        
        if (users.isEmpty()) {
            html.append("        <div>No user data available</div>");
        } else {
            html.append("        <table border='1'>");
            html.append("            <thead>");
            html.append("                <tr>");
            html.append("                    <th>Username</th>");
            html.append("                    <th>Email</th>");
            html.append("                    <th>Registration Time</th>");
            html.append("                    <th>Status</th>");
            html.append("                </tr>");
            html.append("            </thead>");
            html.append("            <tbody>");
            
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            
            for (User user : users) {
                html.append("                <tr>");
                html.append("                    <td>").append(user.getUsername()).append("</td>");
                html.append("                    <td>").append(user.getEmail()).append("</td>");
                html.append("                    <td>").append(user.getCreateTime().format(formatter)).append("</td>");
                html.append("                    <td>").append(user.getStatus() == 1 ? "Active" : "Disabled").append("</td>");
                html.append("                </tr>");
            }
            
            html.append("            </tbody>");
            html.append("        </table>");
        }
        
        html.append("    </div>");
        html.append("</body>");
        html.append("</html>");
        
        return html.toString();
    }
}